package other.edan.intlpass.utils;

import com.rkhd.platform.sdk.exception.CustomConfigException;
import com.rkhd.platform.sdk.exception.EmailException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.EmailBody;
import com.rkhd.platform.sdk.service.CustomConfigService;
import com.rkhd.platform.sdk.service.EmailService;
import other.edan.intlpass.entity.Email;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 发送邮件
 * @Author: 黎炎松
 * @Date: 2025/3/5 09:50
 */
public class SendEmailUtils {
    private static Map<String, String> emailConfig;
    private static final Logger LOGGER = LoggerFactory.getLogger();

    static {
        try {
            emailConfig = CustomConfigService.instance().getConfigSet("CampaignEmailConfig");
            LOGGER.info("邮件发送配置 => " + emailConfig);
            if (emailConfig == null) {
                emailConfig = new HashMap<>();
            }
        } catch (CustomConfigException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 发送邮件
     * @param email
     * @throws EmailException
     */
    public static void sendEmail(Email email) throws EmailException {
        EmailService emailService = EmailService.instance();
        EmailBody body = EmailBody.newBuilder().body(email.getContent()) // 邮件内容
                .subject(email.getSubject()) // 主题
                .smtpHost("smtp.exmail.qq.com") // 邮箱服务器地址（根据实际修改）
                .smtpPort("465") // 端口（根据实际修改）
                .isSSL(true) // 不开启ssl加密
                //.sslProtocols("TLS 1.2")
                .receiverList(email.getReceiverList()) // 收件人列表
                .ccList(email.getCcList()) // 抄送人列表
                .senderUserName(emailConfig.getOrDefault("sendEmail", "Marcom@edan.com")) // 发件人邮箱地址
                .senderUserPass(emailConfig.getOrDefault("sendPwd", "Csmm2025")) // 发件人邮箱密码
                .attachmentList(email.getAttachmentList()) // 附件列表
                .build();
        emailService.sendEmail(body);
    }
}
